import {StorageKey} from "@main/config/const";
//当前用户唯一ID，没有登陆时为0
const uuid = localStorage.getItem("uuid") || "0";

class storage {
  key: string;
  value: any;
  valueFormat: string;
  info: object;

  constructor(key: string, value: any= null) {
    this.key = key;
    this.value = value;
    let values = JSON.parse(localStorage.getItem(StorageKey)) || {};
    this.info = values[uuid] || {};
  };

  private format(){
    let value = this.value;
    switch(typeof(value)){
      case "bigint":
      case "number":
      case "boolean":
      case "string":
        this.valueFormat = value.toString();
        break;
      default:
        break;
      case "object":
        this.valueFormat = JSON.stringify(value);
      break;
      case "function":
      case "symbol":
      case "undefined":
        this.valueFormat = "";
        break;
    }
    return this.valueFormat;
  }

  public get(){
    if(!this.info) return false;
    return  this.info[this.key] || "";
  }

  public set(){
    if(!this.info){
      localStorage.setItem(StorageKey[uuid][this.key], this.valueFormat)
    }
    //TBD 具体实现方法，待完善
    localStorage.setItem(StorageKey[uuid][this.key], this.valueFormat)
  }
}

export default {
  get:function(key:string){
    let s = new storage(key);
    return s.get();
  },

  set:function(key:string,value:any){
    let s = new storage(key,value);
    return s.set();
  }
}
